<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<title>Simple Routing</title>
<link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js/dojo/dijit/themes/tundra/tundra.css">
<script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6"></script>
<script type="text/javascript">
      dojo.require("esri.map");
      dojo.require("esri.tasks.route");

      var map, routeTask, routeParams;
      var stopSymbol, routeSymbol, lastStop;

      function init() {
    	  
    	  map = new esri.Map("map", { extent: new esri.geometry.Extent({ xmin: 700029, ymin: 5661931, xmax: 701878, ymax: 5662838, spatialReference: { wkid: 26911} })});
    	  
        //map = new esri.Map("map", {
        //  extent:new esri.geometry.Extent({xmin:-117.22111701965332,ymin:34.039506912231445,xmax:-117.16961860656738,ymax:34.07383918762207,spatialReference:{wkid:4326}})
        //});

        //uni layer
            	  var tiledMapServiceLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://136.159.79.228/ArcGIS/rest/services/Imagery/TrueOrtho2011_cached/MapServer");
          map.addLayer(tiledMapServiceLayer);

          buildingLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://136.159.79.228/ArcGIS/rest/services/Buildings/MapServer");
          buildingLayer.opacity = 0.4;

          if (buildingLayer.loaded) {
              queryBuilding();
              map.addLayer(buildingLayer);
          }
          else {
              dojo.connect(buildingLayer, "onLoad", function (service) {
                  queryBuilding();
                  map.addLayer(buildingLayer);
              });
          }
          roomsLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://136.159.79.228/ArcGIS/rest/services/Rooms/Rooms/MapServer");

        
        
        //map.addLayer(new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"));
        dojo.connect(map, "onClick", addStop);

        routeTask = new esri.tasks.RouteTask("http://136.159.24.32/ArcGIS/rest/services/UC_NETWORK/NAServer/Route");
        routeParams = new esri.tasks.RouteParameters();
        routeParams.stops = new esri.tasks.FeatureSet();
        routeParams.outSpatialReference = { "wkid": 26911 };
        routeParams.impedanceAttribute = "Length";
        routeParams.returnDirections = true;
        routeParams.directionsLengthUnits = esri.Units.METERS;
        routeParams.outputLines = "esriNAOutputLineTrueShape"; //"esriNAOutputLineNone"; //"esriNAOutputLineStraight";  //"esriNAOutputLineTrueShapeWithMeasure";
        
        
       var x1 = 701192.8861; //700326.68338;
       var y1 = 5662659.7696; //5662241.3256;

       var x2 = 701012.8757; // 701586.12106;
       var y2 = 5662665.3092; //5662819.3331;
        
        
        var startPoint = new esri.Graphic({ "geometry": { "spatialReference": { "wkid": 26911 }
        }, "attributes": { "X": x1, "y": y1, "z": 0.0, "GraphicType": "Stop" }
        });
        
        routeParams.stops.features.push(startPoint);
        
        var stopPoint = new esri.Graphic({ "geometry": { "spatialReference": { "wkid": 26911 }
        }, "attributes": { "X": x2, "y": y2, "z": 16.0, "GraphicType": "Stop" }
        });
        
        routeParams.stops.features.push(stopPoint);
        
        routeTask.solve(routeParams);
        
        //routeTask = new esri.tasks.RouteTask("http://tasks.arcgisonline.com/ArcGIS/rest/services/NetworkAnalysis/ESRI_Route_NA/NAServer/Route");
        //routeParams = new esri.tasks.RouteParameters();
        //routeParams.stops = new esri.tasks.FeatureSet();

        dojo.connect(routeTask, "onSolveComplete", showRoute);
        dojo.connect(routeTask, "onError", errorHandler);

        stopSymbol = new esri.symbol.SimpleMarkerSymbol().setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_CROSS).setSize(15);
        stopSymbol.outline.setWidth(4);
        routeSymbol = new esri.symbol.SimpleLineSymbol().setColor(new dojo.Color([0,0,255,0.5])).setWidth(5);
      }

      dojo.addOnLoad(init);

      //Adds a graphic when the user clicks the map. If 2 or more points exist, route is solved.
      function addStop(evt) {
        var stop = map.graphics.add(new esri.Graphic(evt.mapPoint, stopSymbol));
        routeParams.stops.features.push(stop);

        if (routeParams.stops.features.length >= 2) {
          routeTask.solve(routeParams);
          lastStop = routeParams.stops.features.splice(0, 1)[0];
        }
      }

      //Adds the solved route to the map as a graphic
      function showRoute(routeResults, barriers) {    	
        map.graphics.add(routeResults[0].route.setSymbol(routeSymbol));
        
        var myDiv1 = document.getElementById("div1");
        myDiv1.appendChild(document.createTextNode(dojo.toJson(routeResults[0].route.toJson())));
        
        //dojo.toJson(routeResults[0].route.toJson());
      }

      //Displays any error returned by the Route Task
      function errorHandler(err) {
        alert("An error occured\n" + err.message + "\n" + err.details.join("\n"));

        routeParams.stops.features.splice(0, 0, lastStop);
        map.graphics.remove(routeParams.stops.features.splice(1, 1)[0]);
      }
    </script>
</head>
<body class="tundra">
	<div id = "div1"></div>
	<div id="map" style="width: 600px; height: 400px; border: 1px solid #000;"></div>
	<p>Click on the map to add stops to the route. The route from the last stop to the newly added stop is calculated. If a stop is not reachable, it
		is removed and the last valid point is set as the starting point.</p>
</body>
</html>